package com.lucenly.card.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lucenly.card.R;
import com.lucenly.card.adapter.RankBAdapter;
import com.lucenly.card.base.BaseFragment;
import com.lucenly.card.bean.RankBean;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.GsonUtil;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RankBFragment extends BaseFragment {
    @BindView(R.id.rv_data)
    RecyclerView recyclerView;
    RankBAdapter adapter;
    List<RankBean> list = new ArrayList<>();
    @Override
    protected int getLayoutID() {
        return R.layout.fragment_rank_b;
    }

    @Override
    protected void initView(View contentView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RankBAdapter();
        adapter.replaceData(list);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void bindEvent() {

    }

    @Override
    protected void initData() {

    }
    public void setTop(){
        recyclerView.scrollToPosition(0);
    }
    private void getData() {
        ViseHttp.GET(UrlDefend.GET_RANK)
                .addParam("typeId","2")
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            JSONArray list = jsonObject.getJSONArray("list");
                            if(list!=null){
                                ArrayList<RankBean> rankBeans = GsonUtil.fromJsonList(list.toString(), RankBean.class);
                                adapter.replaceData(rankBeans);
                            }
                            BusManager.getBus().post(new StringMsgEvent("RankFragment"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        ToastUtil.showSingletonShort(errMsg);
                        BusManager.getBus().post(new StringMsgEvent("RankFragment"));
                    }
                });
    }
    @Override
    protected void lazyLoad() {
        getData();
    }

    @Override
    protected void processClick(View view) {

    }

    public void refresh(){
        getData();
    }
}
